package com.project.comom;

import com.common.api.ApprovalApi;
import com.common.core.domain.entity.ProcessConfig;
import com.common.core.domain.entity.SysUser;
import com.common.core.service.ProcessConfigService;
import com.common.utils.SecurityUtils;
import com.common.utils.StringUtils;
import com.flowable.domain.dto.BpmProcessInstanceCreateReqDTO;
import com.flowable.domain.vo.FlowQueryVo;
import com.flowable.service.IFlowInstanceService;
import com.flowable.service.IFlowTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ProcessApiImpl implements ApprovalApi {
//    @Autowired
//    private ProcessConfigMapper baseMapper;
    @Autowired
    private ProcessConfigService processService;

    @Autowired
    private IFlowInstanceService flowInstanceService;

    @Autowired
    private IFlowTaskService flowTaskService;


    /**
     * 业务进入审批
     *
     * @param businessId 业务主表id值
     * @param modelName  模块名
     * @return 流程实例id
     */
    @Override
    public String businessEntryApproval(String businessId, String modelName) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (StringUtils.isEmpty(businessId) || StringUtils.isEmpty(modelName)) return "";
        ProcessConfig pc = processService.getProcessByModelName(modelName);
        BpmProcessInstanceCreateReqDTO reqDTO =new BpmProcessInstanceCreateReqDTO();
        reqDTO.setTableName(pc.getBusinessMainTable());
        reqDTO.setProcessDefinitionKey(pc.getProcessKey());
        reqDTO.setBusinessKey(businessId);

        String processInstanceId = flowInstanceService.createProcessInstance(sysUser.getUserId(),reqDTO);
        return processInstanceId;
    }


    public Map<String, String> getInstanceTaskMap(String moduleName) {
        //获取当前用户所有的代办列表
        ProcessConfig processConfig = processService.getProcessByModelName(moduleName);
        FlowQueryVo queryVo = new FlowQueryVo();
        queryVo.setProcessDefinitionKey(processConfig.getProcessKey());
        // 获取流程任务列表,Map<instanceId, taskId>
        Map<String, String> instanceTaskMap = flowTaskService.todoListBydefKey_Api(queryVo);
        return instanceTaskMap;
    }
}
